﻿using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TAB10
    {
        public TAB10()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABFOLGE();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                ABFME = " ";
                ABFMASCHZE = "  ";

            }
            else
            {
                ABFME = TMV.ABFME;
                ABFMASCHZE = TMV.ABFMASCHZE;
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (LFD == "000000")
            {
                LFD = "";
            }
            dwData = TBMain.NewRow();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = " AND ABFAUFKRS LIKE'" + KRS.Trim() + "%'";
            dwData["ABFAUFLFD"] = " AND ABFAUFLFD LIKE'" + LFD.Trim() + "%'";
            dwData["ABFAUFPOS"] = " AND ABFAUFPOS LIKE'" + POS.Trim() + "%'";
            SResult = TMV.QABF14(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);

        }
        public void RefreshCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                PM["ORDERNUM"] = KRS + LFD + POS;
                PM["AUFTYP"] = "AB05";
                TMV.ABFNEW(PM);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                {
                    throw new Exception("请选择当前生产单行");
                }
                if (dwNAPF == null)
                {
                    throw new Exception("请从步骤中选择");
                }
                int pos = int.Parse(dwMaster["ABFAFOLGE"].ToString());
                TMV.NABF("AB05", dwABP, dwNAPF, Helpers.Helper.Right("000" + (pos + 1).ToString(), 3));
                TMV.RESORT();
                vwMaster.View.MoveCurrentToFirst();
                TBMain.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.NewCommand(sender, e);
        }

        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 3);
            dwData = TBMain.NewRow();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = " AND ABFAUFKRS ='" + KRS + "'";
            dwData["ABFAUFLFD"] = " AND ABFAUFLFD ='" + LFD + "'";
            dwData["ABFAUFPOS"] = " AND ABFAUFPOS ='" + POS + "'";
            SResult = TMV.QABF14(dwData);
            MergeData(SResult, false);
            base.OnEXAUFChg(vlaue);
        }
        public override void OnVU1Chg(string value)
        {
            if (dwMaster == null)
                return;
            if (VU1 == "")
                return;
            TMV.ABFBASMGE = double.Parse(VU1);
            TMV.UpdateABFOLGE(dwMaster, 0);
            base.OnVU1Chg(value);
        }
        public override void OnMACHINEChg(string am)
        {
            if (dwMaster == null)
                return;
            if (am == "")
                return;
            TMV.ABFMNRKRS = am.Substring(0, 2);
            TMV.ABFMLFDNR = am.Substring(2, 4);
            TMV.ABFMPOS = am.Substring(6, 2);
            TMV.UpdateABFOLGE(dwMaster, 0);
            base.OnMACHINEChg(am);
        }
    }
}
